package typoker.logic;

public class Player {
	private static final int startingCash = 100;
	private static final String cardBack = "B.png";

	private final String name;
	private final String image;

	private boolean inGame;
	private boolean inRound;
	private boolean betting;

	private int cash;
	private int controlCash;
	private int bet;
	private int winnings;

	private String[] cards;
	private Card[] hand;
	private int nrOfCards;

	public Player(String name, String image) {
		this.name = name;
		this.image = image;

		inGame = true;
		inRound = true;
		betting = true;

		cash = startingCash;
		controlCash = cash;
		bet = 0;
		winnings = 0;

		cards = new String[2];
		hand = new Card[2];
		nrOfCards = 0;
	}

	public boolean isInGame() {
		return inGame;
	}

	public boolean isInRound() {
		return inRound;
	}

	public boolean isBetting() {
		return betting;
	}

	public String getName() {
		return name;
	}

	public String getImage() {
		return image;
	}

	public int getCash() {
		return cash;
	}

	public int getAssets() {
		return controlCash;
	}

	public int getBet() {
		return bet;
	}
	
	public int getWinnings() {
		return winnings;
	}

	public Card[] getHand() {
		return hand;
	}
	
	public String[] getCards() {
		return cards;
	}
	
	public void showHand() {
		if (nrOfCards == 2) {
			cards[0] = hand[0].getImage();
			cards[1] = hand[1].getImage();
		}
	}

	public void addCard(Card card) {
		if (nrOfCards < 2) {
			cards[nrOfCards] = cardBack;
			hand[nrOfCards++] = card;
		} else {
			System.err.println(name + " has too many cards");
		}
	}
	
	public void fold() {
		inRound = false;
		betting = false;
		
		foldCards();
	}
	
	public void makeBet(int proposedBet) {
		if (proposedBet <= 0 || proposedBet >= controlCash) {
			System.err.println(name + " has made an invalid bet");
			return;
		}
		
		bet = proposedBet;
		cash = controlCash - bet;
	}
	
	public void allin() {
		betting = false;
		
		bet = controlCash;
		cash = 0;
	}
	
	public void loseRound() {
		inRound = false;
		betting = false;
	}
	
	public void leaveGame() {
		inGame = false;
		inRound = false;
		betting = false;
		
		controlCash = 0;
		cash = 0;
		bet = 0;
		
		foldCards();
	}

	public void collectWinnings(int _winnings) {
		if (_winnings < 0) {
			System.err.println(name + " won a negative sum???");
			return;
		}
		
		cash += _winnings;
		winnings += _winnings;
	}

	public void reset() {
		if (inGame) {
			inRound = true;
			betting = true;

			controlCash = cash;
			bet = 0;
			winnings = 0;
			
			foldCards();
		}
	}
	
	private void foldCards() {
		nrOfCards = 0;
		hand[0] = null;
		hand[1] = null;
		
		cards[0] = null;
		cards[1] = null;
	}
}
